package com.fm.demo.joke

import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fm.common.base.BaseRecyclerFragment
import com.fm.demo.bean.JokeBean
import com.fm.demo.bean.JokeEntity
import com.fm.demo.databinding.FragmentJokeHomeBinding
import com.fm.demo.utils.HttpUtils


/**
 * @CreateData:     2019/11/12 14:37
 * @E-Mail:         709674174@qq.com
 * @Author:         lyy
 * @Description:    全部
 */
class JokeHomeFragment : BaseRecyclerFragment<FragmentJokeHomeBinding,JokeBean>() {
    private var isRefresh = false

    companion object {
        const val TYPE_ALL = "all"
        const val TYPE_TEXT = "text"
        const val TYPE_VIDEO = "video"
        const val TYPE_IMAGE = "image"
        const val TYPE_GIF = "gif"

        fun newInstance(type: String): JokeHomeFragment {
            val args = Bundle()
            args.putString("type", type)
            val fragment = JokeHomeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initView() {
        initAdapter(true,10)
        initRefresh()
    }

    override fun request() {
        val params = HashMap<String, Any>()
        params["type"] = getType()
        params["page"] = page
        params["count"] = 10
        HttpUtils.buildPost<JokeEntity>(this,"",params,{
            if (isRefresh) {
                mAdapter?.setList(it?.result)
            } else {
                mAdapter?.addData(it!!.result)
            }
        })
    }

    override fun getAdapter(): BaseQuickAdapter<JokeBean, BaseViewHolder> {
        return JokeItemAdapter()
    }

    private fun getType(): String {
        val type = arguments?.getString("type")
        return if (type.isNullOrEmpty()) {
            TYPE_ALL
        } else {
            type
        }
    }
}
